package com.rongshu.view.mediaPlayer.manager;


import android.media.MediaPlayer;

import com.rongshu.view.mediaPlayer.emnu.MediaState;

import java.io.IOException;

/**
 * created by zhengweis on 2019/4/22.
 * description：
 */
public class AudiobookPlayer extends MediaPlayer implements MediaPlayer.OnCompletionListener {
    private MediaState mState;

    private OnCompletionListener mOnCompletionListener;

    public AudiobookPlayer() {
        super();
        mState = MediaState.IDLE;
        super.setOnCompletionListener(this);
    }

    @Override
    public void setDataSource(String path) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
        super.setDataSource(path);
        mState = MediaState.INITIALIZED;
    }

    @Override
    public void start() {
        super.start();
        mState = MediaState.STARTED;
    }

    @Override
    public void setOnCompletionListener(OnCompletionListener listener) {
        this.mOnCompletionListener = listener;
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mState = MediaState.COMPLETED;
        if (mOnCompletionListener != null) {
            mOnCompletionListener.onCompletion(mp);
        }
    }

    @Override
    public void stop() throws IllegalStateException {
        super.stop();
        mState = MediaState.STOPPED;
    }

    @Override
    public void pause() throws IllegalStateException {
        super.pause();
        mState = MediaState.PAUSED;
    }

    public MediaState getState() {
        return mState;
    }

    public boolean isComplete() {
        return mState == MediaState.COMPLETED;
    }
}
